{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "import hashlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['__all__', '__builtin_constructor_cache', '__builtins__', '__cached__', '__doc__', '__file__', '__get_builtin_constructor', '__loader__', '__name__', '__package__', '__spec__', '_hashlib', 'algorithms_available', 'algorithms_guaranteed', 'blake2b', 'blake2s', 'md5', 'new', 'pbkdf2_hmac', 'scrypt', 'sha1', 'sha224', 'sha256', 'sha384', 'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512', 'sha512', 'shake_128', 'shake_256']\n"
     ]
    }
   ],
   "source": [
    "print(dir(hashlib))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'4be30d9814c6d4e9800e0d2ea9ec9fb00efa887b'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pw = \"123abc\"\n",
    "sh = hashlib.sha1()\n",
    "sh.update(pw.encode())\n",
    "sh.hexdigest()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a906449d5769fa7361d7ecc6aa3f6d28'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pw = \"123abc\"\n",
    "md = hashlib.md5()   #初始化一个hash算法对象，使用md5\n",
    "md.update(pw.encode())  #pw是一个字符串，需要先编码操作\n",
    "md.hexdigest()   #以16进制显示加密结果\n",
    "#output:a906449d5769fa7361d7ecc6aa3f6d28"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "sh.update?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3440d700a7f34720da6049e5605e71e7200aa0953b46a324c78193f1bc4d5c55\n"
     ]
    }
   ],
   "source": [
    "import hashlib\n",
    "\n",
    "def sha256sum(fp): \n",
    "    sh = hashlib.sha256()\n",
    "    with open(fp,\"rb\") as fp:\n",
    "        while True:\n",
    "            content = fp.read(100)\n",
    "            if not content:\n",
    "                break\n",
    "            sh.update(content)\n",
    "    return sh.hexdigest()   \n",
    "if __name__ == \"__main__\":\n",
    "        res = sha256sum(\"cat.7z\")\n",
    "        print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16d3d9b96a8e434b6fec20c7cdd0a0c00f2066b60a7accf986575e31627083b5\n",
      "51445486dc24a8f26cd327d947839072b72a1e1b86ca124ef89b07337b64e69a\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "import hashlib\n",
    "\n",
    "def sha256sum(fp): \n",
    "    sh = hashlib.sha256()\n",
    "    with open(fp,\"rb\") as fp:\n",
    "        content = fp.read()\n",
    "        sh.update(content)\n",
    "    return sh.hexdigest()   \n",
    "if __name__ == \"__main__\":\n",
    "    res = sha256sum(\"cat.jpg\")\n",
    "    res_2 = sha256sum(\"cat_2.jpg\")\n",
    "    print(res,res_2,sep='\\n')\n",
    "    print(res == res_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'dd130a849d7b29e5541b05d2f7f86a4acd4f1ec598c1c9438783f56bc4f0ff80'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pw = \"123abc\"\n",
    "sh = hashlib.sha256()\n",
    "sh.update(pw.encode())\n",
    "sh.hexdigest()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "cat = plt.imread(\"cat.jpg\")\n",
    "cat_2 = np.array(cat)\n",
    "cat_2[0,0,:] = [255,255,255]\n",
    "plt.imsave(\"cat_2.jpg\",cat_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
